package info.touret.winecellar.userinfo.server;

import info.touret.winecellar.userinfo.client.UserInfoService;
import info.touret.winecellar.userinfo.shared.UserInfoDTO;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gdata.client.http.AuthSubUtil;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class UserInfoServiceImpl extends RemoteServiceServlet implements
		UserInfoService {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6028856818956354276L;

	public UserInfoDTO getCurrentUserInfo() {
		UserInfoDTO dto = new UserInfoDTO();

		UserService service = UserServiceFactory.getUserService();
		User current = service.getCurrentUser();
		dto.setEmailAddress(current.getEmail());
		dto.setLoggedIn(service.isUserLoggedIn());
		dto.setLoginURL(service.createLoginURL("Login"));
		dto.setLogoutURL(service.createLogoutURL("Logout"));
		return dto;
	}

	@Override
	public String createLogoutURL(String _value) {
		String url = null;
		UserService service =UserServiceFactory.getUserService();
		url=service.createLogoutURL(_value);
		return url;
	}
	
	public String createGoogleAuthURL(String url){
		StringBuffer buffer = new StringBuffer(url).append("tokenServlet");
		return AuthSubUtil.getRequestUrl(buffer.toString() , "http://www.google.com/calendar/feeds/", false, true);
	}
}
